<?php session_start();
$host = $_SERVER['HTTP_HOST'];
if ($_SERVER['SERVER_NAME'] == 'localhost'){$uri="/kalender";}else{$uri="";}
if (!isset($_SESSION['id_user']) || $_SESSION['id_user']=='') {
	echo "<script language=Javascript> location.href='http://$host$uri/index.php'; </script>";	
}else{
		
	if (!is_object($bd)){	
		//me conecto a la base de datos
		require("../globals/Db.class.php");
		require("../globals/Conf.class.php");
		$bd=Db::getInstance();	
	}
	if (!is_object($util)){	
		include("../globals/Util.class.php");		
		$util=Util::getInstance();
	}
	
	
	
	if (!($util->administrarTiempoSesion())){
		echo "<script language=Javascript> location.href='http://$host$uri/timeout.php'; </script>";
	}else if (!($util->administrarAcceso("facturacion"))){
		echo "<script language=Javascript> location.href='http://$host$uri/sinAcceso.php'; </script>";
	}else{	
		$idOT = $_POST['idOT'];
		$idDoc = $_POST['idDoc'];
		try {
			$bd->beginTransaction();			
				//elimino la relacion de la tabla
				$query = "delete from doc_ot_relation where id_doc=$idDoc and id_ot=$idOT;";
				$bd->eje($query);
				//marco como no facturado el documento
				$query = "update archivos set facturado=0 where id=$idDoc;";
				$bd->eje($query);		
			$bd->commit();
			echo "msg:ok";
		} catch (Exception $e) {	   
		    $bd->rollback();
		    echo "msg:nok";
		}
		
	}
}
?>